#!/bin/bash

HERE=`pwd`
test -n "$LINUX" || LINUX=linux-2.6.28
test -n "$BBOX"  || BBOX=busybox-1.13.2
BZIMG=$LINUX/arch/$ARCH/boot/bzImage
BLDDIR=bld/$ARCH
CUR_ROOTFS=$BLDDIR/rootfs.config
COMPILE_CODE=1

mkdir -p $BLDDIR

cleanme()
{
  rm -rf $BLDDIR/$LINUX $BLDDIR/$BBOX
  rm -f busybox-$ARCH.exe rootfs-$ARCH.cpio.gz
  rm -f zImage-$ARCH bzImage-$ARCH
  rm -f $CUR_ROOTFS
  exit 0
}

packme()
{
OUT_FILE=$CMDARGS;shift; rm -f $OUT_FILE
SEPERATOR=tmp.123.456.789

files=
for arg in $*; do
  test -d $arg || files="$files $arg"
  test -d $arg && files="$files `find $arg -type f | xargs`"
done

for i in $files; do
cat >> $OUT_FILE << $SEPERATOR

mkdir -p `dirname $i`
`echo -n "cat >> $i << $SEPERATOR"`
`cat $i`
`echo -n $SEPERATOR`

$SEPERATOR
done
exit 0
}

linkme()
{
  rm -f $2
  if [ -f $1 ]; then
    ln -s $1 $2
  fi
}

test "$CMDARGS" = "clean" && cleanme
test "$CMDARGS" = "pack" && packme package.dist configs etc # later - build.sh
test "$CMDARGS" = "rootfs" && COMPILE_CODE=0

mkdir -p Downloads
test -d $BLDDIR/$LINUX || test -f Downloads/$LINUX.tar.bz2 || wget http://www.kernel.org/pub/linux/kernel/v2.6/$LINUX.tar.bz2 -O Downloads/$LINUX.tar.bz2
test -d $BLDDIR/$LINUX || tar -C $BLDDIR -jxf Downloads/$LINUX.tar.bz2
test -d $BLDDIR/$BBOX || test -f Downloads/$BBOX.tar.bz2 || wget http://www.busybox.net/downloads/$BBOX.tar.bz2 -O Downloads/$BBOX.tar.bz2
test -d $BLDDIR/$BBOX || tar -C $BLDDIR -jxf Downloads/$BBOX.tar.bz2

if [ ! -f $BLDDIR/$LINUX/.config ]; then
  if [ -f configs/$LINUX.$ARCH.config ]; then
    cp -f configs/$LINUX.$ARCH.config $BLDDIR/$LINUX/.config
    make -C $BLDDIR/$LINUX silentoldconfig 
  else
    #make -C $BLDDIR/$LINUX defconfig
    make -C $BLDDIR/$LINUX allnoconfig
    #make -C $BLDDIR/$LINUX allnoconfig
  fi
fi
[ $COMPILE_CODE -ne 0 ] && make -C $BLDDIR/$LINUX bzImage
[ $COMPILE_CODE -ne 0 ] && make -C $BLDDIR/$LINUX modules
[ $COMPILE_CODE -ne 0 ] && make -C $BLDDIR/$LINUX/usr gen_init_cpio
linkme $BLDDIR/$LINUX/arch/$ARCH/boot/bzImage bzImage-$ARCH
linkme $BLDDIR/$LINUX/arch/$ARCH/boot/zImage zImage-$ARCH

if [ ! -f $BLDDIR/$BBOX/.config ]; then
  if [ -f configs/$BBOX.$ARCH.config ]; then
    cp -f configs/$BBOX.$ARCH.config $BLDDIR/$BBOX/.config
    make -C $BLDDIR/$BBOX silentoldconfig 
  else
    make -C $BLDDIR/$BBOX defconfig
  fi
fi
[ $COMPILE_CODE -ne 0 ] && make -C $BLDDIR/$BBOX CONFIG_STATIC=y

linkme $BLDDIR/$BBOX/busybox busybox-$ARCH.exe
cat configs/rootfs.config | sed "s/busybox.exe/busybox-$ARCH.exe/g" > $CUR_ROOTFS
for i in `ls etc/programs | xargs`; do
  echo "file /programs/$i etc/programs/$i 755 0 0 " >> $CUR_ROOTFS
done
if [ -f etc/init.d/rcS.$ARCH ]; then
  echo "file /etc/init.d/rcS etc/init.d/rcS.$ARCH 755 0 0" >> $CUR_ROOTFS
else
  echo "file /etc/init.d/rcS etc/init.d/rcS 755 0 0" >> $CUR_ROOTFS
fi
if [ -f etc/inittab.$ARCH ]; then
  echo "file /etc/inittab etc/inittab.$ARCH 755 0 0" >> $CUR_ROOTFS
else
  echo "file /etc/inittab etc/inittab 755 0 0" >> $CUR_ROOTFS
fi

if [ -f $BLDDIR/$BBOX/busybox.links ]; then
  for i in `cat $BLDDIR/$BBOX/busybox.links | xargs`; do
    echo "slink $i /bin/busybox 755 0 0 " >> $CUR_ROOTFS
  done
fi
set -x
$BLDDIR/$LINUX/usr/gen_init_cpio $CUR_ROOTFS | gzip -f -9 > rootfs-$ARCH.cpio.gz

